<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        clearable
        class="filter-item"
        :placeholder="$t('sys_log.placeholder.filter_name')"
      />
      <el-button
        v-permission="['GET /admin/log/list']"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >{{ $t("app.button.search") }}</el-button
      >
      <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('app.message.list_loading')"
      border
      fit
    >
      <el-table-column
        align="center"
        :label="$t('sys_log.table.admin')"
        prop="admin"
        fixed="left"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('sys_log.table.ip')"
        prop="ip"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        width="170"
        align="center"
        :label="$t('sys_log.table.add_time')"
        prop="addTime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('sys_log.table.type')"
        prop="type"
        min-width="100"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-tag>{{ scope.row.type | typeFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('sys_log.table.action')"
        prop="action"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('sys_log.table.status')"
        prop="status"
        fixed="right"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-tag :type="scope.row.status ? 'success' : 'error'">{{
            $t(
              scope.row.status
                ? "sys_log.value.status_success"
                : "sys_log.value.status_error"
            )
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('sys_log.table.result')"
        prop="result"
        min-width="200"
        fixed="right"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('sys_log.table.comment')"
        prop="comment"
        show-overflow-tooltip
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listLog } from "@/api/log";
import Pagination from "@/components/Pagination";

const typeMap = {
  0: "一般操作",
  1: "安全操作",
  2: "订单操作",
  3: "其他操作",
};

export default {
  name: "Log",
  components: { Pagination },
  filters: {
    typeFilter(type) {
      return typeMap[type];
    },
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        sort: "add_time",
        order: "desc",
      },
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      listLog(this.listQuery)
        .then((response) => {
          this.list = response.data.data.list;
          this.total = response.data.data.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.list = [];
          this.total = 0;
          this.listLoading = false;
        });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetQuery() {
      this.listQuery = {
        page: 1,
        limit: 20,
        name: undefined,
      };
      this.getList();
    },
  },
};
</script>
